<?php
	namespace app\index\controller;

	class Upload extends Base{
		public function index(){

			try{

				if(isset($_FILES['file'])){

				$types=['.jpg','.bmp','.jpeg','.png','.gif'];

				$type=strtolower(strrchr($_FILES['file']['name'],'.'));

				if(!in_array($type,$types)){
					die(json_encode(['msg'=>'请上传后缀为'.join($types,',').'的图片','code'=>0]));
				}

				$file=request()->file('file');

				$upload_path=str_replace('\\','/',ROOTPATH.'/temp');

				$save_path='/temp/';

				$info=$file->move($upload_path);

				if($info){
					$filename ='//'.$_SERVER['HTTP_HOST'].str_replace('\\', '/', $save_path .  $info->getSaveName());

					$arr=['code'=>1,'src'=>$filename,'msg'=>'上传成功'];
				}else{
					$arr=['code'=>0,'msg'=>'上传失败'];
				}


				echo json_encode($arr);
			}
		}catch(\Exception $e){
			return $this->err($e);
		}
			
			
		}

		function scenic(){
			/*try{*/
				if($_FILES){
					$file=$_FILES['file'];

					if($file['error']>0){
						return $this->fall('上传失败');
					}

					$type=['image/jpg','image/jpeg','image/png','image/gif'];

					if(!in_array($file['type'],$type)){
						return $this->fail('上传类型需为图片');
					}

					$size=$file['size']/1024/1024;

					if($size>3){
						return $this->fail('图片大小不能超过3MB');
					}

					$hz=strrchr($file['name'],'.');

					$name='/public/images/temp/scenic-icon'.md5(rand(10,99)).$hz;

					$src='.'.$name;

					if(move_uploaded_file($file['tmp_name'],$src)){
						$name='//'.$_SERVER['HTTP_HOST'].$name;

						return $this->success(['src'=>$name]);
					}else{
						return $this->fail('上传失败');
					}
				}
			/*}catch(\Exception $e){
				return $this->err($e);
			}*/
		}

		public function deleFile(){
			try{
				if($_POST){
					$src=input('post.src');

					$types=['.jpg','.bmp','.jpeg','.png','.gif','.avi','.rmvb','.rm','.asf','.divx','.mpg','.mpeg','.mpe','.wmv','.mp4','.mkv','.vob'];

					$type=strtolower(strrchr($src,'.'));

					if($src=='' || !in_array($type,$types) || !is_file('.'.$src)){
						die(json_encode(['msg'=>'操作失败','code'=>0]));
					}

					$src=str_replace('//'.$_SERVER['HTTP_HOST'],'', $src);

					@unlink('.'.$src);

					echo json_encode(['msg'=>'操作成功','code'=>1]);


				}else{
					echo '拒绝访问';
				}
			}catch(\Exception $e){
				Log::write($e->getMessage(),'ERR');

				echo json_encode(['msg'=>'系统繁忙','code'=>0]);
			}
		}
		
	}
?>